$(function () {
    var id = $("#zjjhgl_zjjhid").val();
    var type = $("#zjjhgl_type").val();
    //自购资金计划列
    var zgzjjhmxCol = [
        {title: '付款日期', field: 'fkrq', width: 150, align: 'center'},
        {title: '付款金额（元）', field: 'fkje', align: 'right', formatter: function (value) {
            var sl = formatJeForHtml2(value, ",");
            return sl;
        }, width: 150}
    ];
    //统配资金计划列
    var tpzjjhmxCol = [
        {title: '品名', field: 'pm.name', align: 'center', width: 150},
        {title: '核定数量', field: 'hdsl', align: 'right', width: 150},
        {title: '付款日期', field: 'fkrq', width: 150, align: 'center'},
        {title: '付款金额（元）', field: 'fkje', align: 'right',
            formatter: function (value) {
                var sl = formatJeForHtml2(value, ",");
                return sl;
            }, width: 150}
    ];

    //借款单
    var jkdCol = [
        {title: '付款日期', field: 'zfsj', align: 'center', width: 130},
        {title: '借款单号', field: 'jkdbm', align: 'center', width: 160},
        {title: '供应商', field: 'gys_qc', align: 'center', width: 150},
        {title: '支付款项', field: 'zfkx', align: 'center', width: 90},
        {title: '支付方式', field: 'zffs_z', align: 'center', width: 70},
        {title: '借款金额（元）', align: 'right', field: 'zfje',
            formatter: function (value) {
                var sl = formatJeForHtml2(value, ",");
                return sl;
            }, width: 280},
        {title: '已付款', field: 'yfje', align: 'right',
            formatter: function (value) {
                var sl = formatJeForHtml2(value, ",");
                return sl;
            }, width: 280},
        {title: '借款公司', field: 'jkgs_qc', align: 'center', width: 150},
        {title: '申报公司', field: 'sbgs_qc', align: 'center', width: 100},
        {title: '借款人', field: 'createUser_xm', align: 'center', width: 70},
        {title: '申请日期', field: 'dateCreated', align: 'center', width: 130},
        {title: '状态', field: 'zt', width: $(this).width() * 0.07, align: 'center',
            formatter: formatJkdZT
        }
    ];

    var zjjhmxCol = null;

    if (type == "Tpzjjh") {
        zjjhmxCol = tpzjjhmxCol;
    }

    if (type == "Zgzjjh") {
        zjjhmxCol = zgzjjhmxCol;
    }

    $("#zjjhgl_zjjhmx_grid").datagrid({
        rownumbers: "true",
        singleSelect: "true",
        showFooter: true,
        fitColumns: true,
        fit: true,
        url: $.webPath() + "/jhgl/zjjhgl/zjjhmxList?id=" + id + "&type=" + type,
        columns: [zjjhmxCol],
        onLoadSuccess: function () {
            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        }
    });

    $("#zjjhgl_jkd_grid").datagrid({
        toolbar: "#zjjhgl_jkd_toolbar",
        rownumbers: "true",
        singleSelect: "true",
        fitColumns: true,
        fit: true,
        url: $.webPath() + "/jhgl/zjjhgl/jkdList?id=" + id + "&type=" + type,
        columns: [jkdCol],
        onLoadSuccess: function () {
            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        }
    });
    // 删除电子借款单
    $("#zjjhgl_jkd_toolbar [name='scjkd']").click(function () {
        var row = $('#zjjhgl_jkd_grid').datagrid("getSelected");
        if (row) {
            if (row.yfje != 0) {
                $.messager.alert("提示", "请选择未付款的借款单!", "warning");
                return;
            }
            $.messager.confirm('确认', '是否确认删除此借款单?', function (r) {
                if (r) {
                    zjjhAjax("/jhgl/jkd/delete_jkd", row.id);
                }
            });
        }
        else {
            $.messager.alert("提示", "请先选择借款单!", "warning");
        }

    });

    //资金计划明细导出
    $("#btn_zijhmx_outport").click(function () {
        //window.open($.webPath() + "/xsgl/kcbj/zijhmx_export?bjgsId="+$('#bj_bjgs').combobox('getValue')+"&&bjrq="+ $('#bj_bjrq').combobox('getValue'));
    });

    //提交审核电子借款单
    $("#zjjhgl_jkd_toolbar [name='tjjkd']").click(function () {
        var row = $('#zjjhgl_jkd_grid').datagrid("getSelected");
        if (row) {
            if (row.zt == 'ytj' || row.zt == 'ysp') {
                $.messager.alert("提示", "请选择可提交的借款单!", "warning");
                return;
            }
            $.messager.confirm('确认', '是否确认提交此借款单计划?', function (r) {
                if (r) {
                    zjjhAjax("/jhgl/jkd/commit_jkd", row.id);
                }
            });
        }
        else {
            $.messager.alert("提示", "请先选择借款单!", "warning");
        }

    });

    function zjjhAjax(url, jkdId) {
        $.ajax({
            url: $.webPath() + url,
            dataType: 'json',
            type: 'POST',
            data: {
                jkdId: jkdId
            },
            beforeSend: function () {
                $("#zjjhgl_layout").mask();
            },
            success: function (data) {
                $("#zjjhgl_layout").mask("hide");
                if (data.statusCode == 200) {
                    $('#zjjhgl_jkd_grid').datagrid("reload");
                    //reloadZjjhglGrid();
                    $.messager.show({
                        title: '提示',
                        msg: data.message,
                        timeout: 3000,
                        showType: 'slide'
                    });
                } else {
                    $.messager.alert('提示', data.message, 'error');
                }
            },
            error: function () {

            }
        });
    }
});

function formatJkdZT(val, row) {
    if (val == 'ybc')
        val = '已保存';
    if (val == 'ytj')
        val = '已提交';
    if (val == 'ysp')
        val = '已审批';

    if (val == '已保存') {
        return val;
    } else {
        return '<a href="javascript:viewJkdFlowPicture();" style="color: red">' + val + '</a>';
    }
}

function viewJkdFlowPicture() {
    var row = $('#zjjhgl_jkd_grid').datagrid("getSelected");
    $.window({
        title: "流程历史",
        iconCls: 'icon-add2',
        url: $.webPath() + "/flow/publish/flowPicture?processInstanceId=" + encodeURI(encodeURI(row.processInstanceId)),
        width: 650,
        height: 500,
        closed: true,
        modal: true,
        isMax: true
    });
}

function viewZjjhglXyDetail(xyId, xyType) {
    if (xyType != null && xyType == "Cgxy") {
        var type = 1;
    } else if (xyType != null && xyType == "Xsxy") {
        type = 2;
    } else if (xyType != null && xyType == "Sfxy") {
        type = 3;
    }
    $.window({
        title: '详细信息',
        iconCls: 'icon-tip',
        url: $.webPath() + "/jhgl/xygl/xygl_detail?type=" + type + "&xyId=" + xyId,
        width: 1050,
        height: 450
    })
}

function viewZjjhglWzjhDetail(wzjhId, wzjhType) {
    $.window({
        title: '详细信息',
        iconCls: 'icon-tip',
        url: $.webPath() + "/jhgl/wzjhgl/wzjhgl_detail?id=" + wzjhId + "&type=" + wzjhType,
        width: 1050,
        height: 450
    })
}